<?php
//前台访问控制器
namespace app\expbao\home;
use app\expbao\common\controller\ExApi;
use app\expbao\model\ExUser as ExUserModel;
use app\expbao\model\ExOpt as ExOptModel;
use app\expbao\model\ExOptUser as ExOptUserModel;
use app\expbao\model\ExLiner as ExLinerModel;
use app\expbao\model\ExBoard as ExBoardModel;
use think\Hook;

class Api extends ExApi
{
//前台访问控制器
    public function index()
    {
         $eid=EID;
         $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
         $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
         $request= \think\Request::instance();
          $data = ['content'=>'欢迎使用配送宝api系统','url'=>'www.baidu.com','orderSn'=>$orderSn];
        // 指定json数据输出
          return json(['data'=>$data,'code'=>1,'message'=>'操作成功','hints'=>$this->gethints()]);
    }
    public function get($value='')
    {
      if ($this->request->isPost()) {
          
          $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
        // 指定json数据输出
        return json(['data'=>$data,'code'=>1,'message'=>'操作完成','hints'=>$this->gethints()]);

      } else{
         
          return json(['data'=>'登录页面默认返回数据','code'=>1,'message'=>'操作成功','hints'=>$this->gethints()]);

      }

    }
        public function login()
    {
        
        if ($this->request->isPost()) {
            // 获取post数据
            $data = $this->request->post();
            $rememberme = isset($data['remember-me']) ? true : false;
            if(isset($data['username'])||isset($data['password'])){
                  $UserModel = new ExUserModel;
                  $uid = $UserModel->login($data['username'], $data['password'], $rememberme);
                  $datalist=['userid'=>$uid];
                  if ($uid) {
                      // 记录行为
                      //action_log('user_signin', 'admin_user', $uid, $uid);
                      return json(['data'=>$datalist,'code'=>1,'message'=>'登录成功','hints'=>$this->gethints()]);
                  } else {
                      //$this->error($UserModel->getError());
                      $msg=$UserModel->getError();
                      //$datalist=['userid'=>$uid];
                      $hints = ['userid'=>false,'islogin'=>false,'userinfo'=>null];
                      return json(['data'=>$datalist,'code'=>-1,'message'=>$msg,'hints'=>$hints]);
                  }
              }else{
               
                return json(['data'=>'缺少username或者password参数','code'=>0,'message'=>'登录失败','hints'=>$this->gethints()]);
            }
 
                
          }
          else{
            $hints = ['userid'=>false,'islogin'=>false,'userinfo'=>null];
            return json(['data'=>'登录页面默认返回数据','code'=>1,'message'=>'操作成功','hints'=>$hints]);

             }
    }
    public function logout()
    {
        session(null);
        cookie('eid', null);
        cookie('signin_token', null);
        $hints = ['userid'=>false,'islogin'=>false,'userinfo'=>null];
        return json(['data'=>'注销成功','code'=>1,'message'=>'注销成功!','hints'=>$hints]);
    }
    public function reg($value='')
    {
        $hints = ['userid'=>false,'islogin'=>false,'userinfo'=>null];
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $data['member_nickname']='用户'.$data['member_name'];
            $result = $this->validate($data, 'ExUser');
            // 验证失败 输出错误信息
            if(true !== $result) {
              $hints = ['userid'=>false,'islogin'=>false,'userinfo'=>null];
              return json(['data'=>'验证器失败','code'=>-1,'message'=>$result,'hints'=>$hints]);
                }

            if ($user = ExUserModel::create($data)) {
                // 记录行为
                //action_log('member_add', 'member', $user['id'], UID);
                  $opts = ExOptModel::column('id');
                  $opt_reg=implode(",", $opts);
                  $useropt = ExOptUserModel::create([
                            'uid'  =>  $user['id'],
                            'oid' =>  $opt_reg,
                            'isactive' => '1'
                        ]);
                $data=['username'=>$user['member_name'],'userID'=>$user['id']];
                
              return json(['data'=>$data,'code'=>1,'message'=>'新增成功','hints'=>$hints]);
                
            } else {
                return json(['data'=>$user['member_name'],'code'=>-1,'message'=>'新增失败','hints'=>$hints]);
            }
        } else{
            return json(['data'=>'注册页面默认返回数据','code'=>1,'message'=>'操作成功','hints'=>$hints]);
        }
    }

        public function getlinerinfo($value='')
    {
      if ($this->request->isPost()) {
          //$LinerModel = new ExUserModel;
          $data = ExLinerModel::getlinerinfobyall();
          //$data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
        // 指定json数据输出
        return json(['data'=>$data,'code'=>1,'message'=>'操作完成','hints'=>$this->gethints()]);

      } else{
         
          return json(['data'=>'获取发货信息接口','code'=>1,'message'=>'操作成功','hints'=>$this->gethints()]);

      }

    }
        public function getboard()
    {
      if ($this->request->isPost()) {
          //$LinerModel = new ExUserModel;
          $postdata = $this->request->post();
          if ($this->is_exp_login()) {
             $data = ExBoardModel::getboardbyid($postdata['userid']);
          //$data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
        // 指定json数据输出
        return json(['data'=>$data,'code'=>1,'message'=>'操作完成','hints'=>$this->gethints()]);
          }else{

              return json(['data'=>null,'code'=>1,'message'=>'用户未登录','hints'=>$this->gethints()]);
          }


      } else{
         
          return json(['data'=>'获取通知信息接口','code'=>1,'message'=>'操作成功','hints'=>$this->gethints()]);

      }

    }


}